/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package guiutilities;

import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import org.jdesktop.application.ResourceMap;

/**
 *
 * @author tommy
 */
public class jFourDoubleArgumentDialog extends JDialog {
	private boolean confirmed;
	private Double arg1, arg2, arg3, arg4;
	private JLabel jArg2Label, jArg1Label, jArg3Label, jArg4Label, jAux;
	private JTextField jArg2Text, jArg1Text, jArg3Text, jArg4Text;
	private JButton jConfirmOperation;
	
	
	public jFourDoubleArgumentDialog(JFrame parent, ResourceMap resourceMap, String labelArg1, String labelArg2,
		String labelArg3, String labelArg4) {
		// Dialog for browsing and storing 2 images to perform operations among
		// them
		super(parent,"Supply Arguments", true);
		
		getContentPane().setLayout(new SpringLayout());
		setPreferredSize(new Dimension(512,224));
		setMaximumSize(new Dimension(512,224));
		setResizable(false);
		setSize(512, 224);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		jArg2Label = new JLabel(labelArg2 + ": ");
		jArg1Label = new JLabel(labelArg1 + ": ");
		jArg3Label = new JLabel(labelArg3 + ": ");
		jArg4Label = new JLabel(labelArg4 + ": ");
		jAux = new JLabel();
		
		jArg2Text = new JTextField();
		jArg2Text.setMaximumSize(new Dimension(200, 27));
		jArg2Text.setPreferredSize(new Dimension(200, 27));

		jArg1Text = new JTextField();
		jArg1Text.setMaximumSize(new Dimension(200, 27));
		jArg1Text.setPreferredSize(new Dimension(200, 27));
		
		jArg3Text = new JTextField();
		jArg3Text.setMaximumSize(new Dimension(200, 27));
		jArg3Text.setPreferredSize(new Dimension(200, 27));
		
		jArg4Text = new JTextField();
		jArg4Text.setMaximumSize(new Dimension(200, 27));
		jArg4Text.setPreferredSize(new Dimension(200, 27));
		
		

		jConfirmOperation = new JButton();
        jConfirmOperation.setText(resourceMap.getString("jConfirmOperation.text")); // NOI18N
        jConfirmOperation.setName("jConfirmOperation"); // NOI18N
		jConfirmOperation.setPreferredSize(new Dimension(50,50));
		jConfirmOperation.setMinimumSize(new Dimension(50,50));
        jConfirmOperation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jConfirmOperationActionPerformed(evt);
            }
        });
		
		// Add components to Dialog
		getContentPane().add(jArg1Label);
		getContentPane().add(jArg1Text);
		getContentPane().add(jArg2Label);
		getContentPane().add(jArg2Text);
		getContentPane().add(jArg3Label);
		getContentPane().add(jArg3Text);
		getContentPane().add(jArg4Label);
		getContentPane().add(jArg4Text);
		getContentPane().add(jAux);
		getContentPane().add(jConfirmOperation);
		
		SpringUtilities.makeCompactGrid(getContentPane(), 5,2,6,6,6, 6);
	}
	
	public boolean isConfirmed() {
		return this.confirmed;
	}
	
	public double getInputArg1() {
		return this.arg1;
	}
	
	public double getInputArg2() {
		return this.arg2;
	}
	
	public double getInputArg3() {
		return this.arg3;
	}
	
	public double getInputArg4() {
		return this.arg4;
	}
		
	private void jConfirmOperationActionPerformed(java.awt.event.ActionEvent evt) {                                                     
		boolean verified = false;

		try {
			this.arg2 = Double.parseDouble(jArg2Text.getText());
			this.arg1 = Double.parseDouble(jArg1Text.getText());
			this.arg3 = Double.parseDouble(jArg3Text.getText());
			this.arg4 = Double.parseDouble(jArg4Text.getText());

			if (this.arg2 < 0 || this.arg1 < 0 || this.arg3 < 0 || this.arg4 < 0) {
				 // Fuerza que se capture una excepcion para volver a pedirle al usuario input
				throw new NumberFormatException();
			}
			verified = true;
		} catch (NumberFormatException e) {
			verified = false;
			JOptionPane.showMessageDialog(this,"Only positive real numbers accepted");
		}
		
		if (verified) {
			this.dispose();
		}
	}
}
